import React from 'react';

import ReactEcharts from 'echarts-for-react';
import { LineProps } from '../chart.interface';

export class Bar2LineChart extends React.Component<LineProps, {}> {
  static defaultProps = {
    categories: [],
    series: [],
  };

  constructor(props: LineProps) {
    super(props);
  }

  prepareOption = (): any => {
    const option = {
      color: ['#003366', '#006699', '#4cabce', '#e5323e'],
      tooltip: {
        trigger: 'axis',
      },
      grid: {
        top: '5%',
        left: '10%',
        right: '4%',
        bottom: '3%',
      },
      xAxis: {
        show: false,
        type: 'category',
        data: this.props.categories,
      },
      yAxis: {
        show: false,
        type: 'value',
        axisLabel: {
          formatter: '{value} °C',
        },
      },
      series: [] as any,
    };

    for (const s of this.props.series!) {
      option.series.push({
        name: s.name,
        type: 'bar',
        data: s.data,
        markLine: {
          label: {
            position: 'insideStartTop',
          },
          lineStyle: {
            color: 'red',
          },
          data: [{ type: 'average', name: '平均值' }],
        },
      });
    }
    return option;
  };

  render() {
    return (
      <ReactEcharts option={...this.prepareOption()} style={{ height: '140px', width: '100%' }} />
    );
  }
}
